/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class MySQLDataAccessHelper 
{
    private  Connection connection;

    public Connection getConnection() 
    {
        return connection;
    }    
    public void Open()
    {        
        try
        {            
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            String conString="jdbc:mysql://localhost:3306/SieuThiDienMay";
            Properties info = new Properties(); 
            info.setProperty("characterEncoding", "utf8"); 
            info.setProperty("user", "root");
            this.connection = DriverManager.getConnection(conString, info);
        }
        catch(Exception ex)
        {
             System.out.println(ex.getMessage());
        }        
    }
    public void Close()
    {
        try
        {
            this.connection.close();
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
    }
    public ResultSet ExeCuteQuery(String sql)
    {
        ResultSet temp = null;
        try
        {
            Statement statement = this.connection.createStatement();
            temp = statement.executeQuery(sql);            
        }
        catch(Exception ex)
        {            
            System.out.println(ex.getMessage());
        }
        return temp;
    }       
    public int ExeCuteUpdate(String sql)
    {
        int n = -1;
        try
        {
            Statement sm = this.connection.createStatement();
            n = sm.executeUpdate(sql);
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return n;
    }
}
